/**
 * description:
 * fileName: DrawUtisl.ts
 * author: ssr
 * date: 2023-01-10 09:44:25
 * version: V1.0.0
 */
import {
  BoundingSphere,
  Cartesian3,
  Cartographic,
  EllipsoidGeodesic,
  Rectangle,
} from "cesium";
import Entity from "cesium/Source/DataSources/Entity";
import { IPoint2D } from "../CustomCesium";

export default class DrawUtils {
  /**
   * 获取椭圆长短半径
   * @param positions 两个点 第一个中心点
   * @returns
   */
  static getEllipseRadius(positions: Cartesian3[]) {
    let _majorAxis = 0,
      _minorAxis = 0;
    if (positions.length === 2) {
      const start = Cartographic.fromCartesian(positions[0]);
      const end = Cartographic.fromCartesian(positions[1]);
      // 长半轴
      _majorAxis = Cartesian3.distance(positions[0], positions[1]);
      var geodesic = new EllipsoidGeodesic();
      geodesic.setEndPoints(
        start,
        new Cartographic(start.longitude, end.latitude)
      );
      // 短半轴
      _minorAxis = geodesic.surfaceDistance;
    }

    return {
      majorAxis: _majorAxis,
      minorAxis: _minorAxis,
    };
  }

  /**
   * 获取多边形中心点坐标
   * @param positions
   */
  static getPolygonCenter(
    positions: Cartesian3[],
    entity?: Entity
  ): Cartesian3 | null {
    if (entity && entity.polygon && entity.polygon.hierarchy) {
      //中心点
      var pCenter = BoundingSphere.fromPoints(positions).center;
      return pCenter;
    }
    return null;
  }

  /**
   * 获取矩形中心点坐标
   * @param positions
   */
  static getRectangleCenter(positions: Cartesian3[]): Cartesian3 | null {
    const positionsClone = positions.map((car3) => car3.clone()),
      rectangle = Rectangle.fromCartesianArray(positionsClone),
      center = Rectangle.center(rectangle);
    return Cartographic.toCartesian(center);
  }
}
